In [1]:
%load_ext autoreload
%autoreload 2
In [2]:
import xarray as xr
import pandas as pd
import hvplot.pandas
import hvplot.xarray
In [3]:
varname = 'salinity'
In [4]:
ds = xr.open_mfdataset(f'../tests/data/m1_hello_schism/outputs/{varname}_*.nc', concat_dim='time', combine="nested",
                  data_vars='minimal', coords='minimal', compat='override')
ds
Out[4]:
<xarray.Dataset>
Dimensions:   (time: 144, nSCHISM_hgrid_node: 2639, nSCHISM_vgrid_layers: 11)
Coordinates:
  * time      (time) datetime64[ns] 1999-12-31T16:20:00 ... 2000-01-02T16:00:00
Dimensions without coordinates: nSCHISM_hgrid_node, nSCHISM_vgrid_layers
Data variables:
    salinity  (time, nSCHISM_hgrid_node, nSCHISM_vgrid_layers) float32 dask.array<chunksize=(72, 2639, 11), meta=np.ndarray>
xarray.Dataset
    • time: 144
    • nSCHISM_hgrid_node: 2639
    • nSCHISM_vgrid_layers: 11
    • time
      (time)
      datetime64[ns]
      1999-12-31T16:20:00 ... 2000-01-...
      i23d :
      0
      base_date :
      2000 1 1 0.00 8.00
      standard_name :
      time
      axis :
      T
      array(['1999-12-31T16:20:00.000000000', '1999-12-31T16:40:00.000000000',
             '1999-12-31T17:00:00.000000000', '1999-12-31T17:20:00.000000000',
             '1999-12-31T17:40:00.000000000', '1999-12-31T18:00:00.000000000',
             '1999-12-31T18:20:00.000000000', '1999-12-31T18:40:00.000000000',
             '1999-12-31T19:00:00.000000000', '1999-12-31T19:20:00.000000000',
             '1999-12-31T19:40:00.000000000', '1999-12-31T20:00:00.000000000',
             '1999-12-31T20:20:00.000000000', '1999-12-31T20:40:00.000000000',
             '1999-12-31T21:00:00.000000000', '1999-12-31T21:20:00.000000000',
             '1999-12-31T21:40:00.000000000', '1999-12-31T22:00:00.000000000',
             '1999-12-31T22:20:00.000000000', '1999-12-31T22:40:00.000000000',
             '1999-12-31T23:00:00.000000000', '1999-12-31T23:20:00.000000000',
             '1999-12-31T23:40:00.000000000', '2000-01-01T00:00:00.000000000',
             '2000-01-01T00:20:00.000000000', '2000-01-01T00:40:00.000000000',
             '2000-01-01T01:00:00.000000000', '2000-01-01T01:20:00.000000000',
             '2000-01-01T01:40:00.000000000', '2000-01-01T02:00:00.000000000',
             '2000-01-01T02:20:00.000000000', '2000-01-01T02:40:00.000000000',
             '2000-01-01T03:00:00.000000000', '2000-01-01T03:20:00.000000000',
             '2000-01-01T03:40:00.000000000', '2000-01-01T04:00:00.000000000',
             '2000-01-01T04:20:00.000000000', '2000-01-01T04:40:00.000000000',
             '2000-01-01T05:00:00.000000000', '2000-01-01T05:20:00.000000000',
             '2000-01-01T05:40:00.000000000', '2000-01-01T06:00:00.000000000',
             '2000-01-01T06:20:00.000000000', '2000-01-01T06:40:00.000000000',
             '2000-01-01T07:00:00.000000000', '2000-01-01T07:20:00.000000000',
             '2000-01-01T07:40:00.000000000', '2000-01-01T08:00:00.000000000',
             '2000-01-01T08:20:00.000000000', '2000-01-01T08:40:00.000000000',
             '2000-01-01T09:00:00.000000000', '2000-01-01T09:20:00.000000000',
             '2000-01-01T09:40:00.000000000', '2000-01-01T10:00:00.000000000',
             '2000-01-01T10:20:00.000000000', '2000-01-01T10:40:00.000000000',
             '2000-01-01T11:00:00.000000000', '2000-01-01T11:20:00.000000000',
             '2000-01-01T11:40:00.000000000', '2000-01-01T12:00:00.000000000',
             '2000-01-01T12:20:00.000000000', '2000-01-01T12:40:00.000000000',
             '2000-01-01T13:00:00.000000000', '2000-01-01T13:20:00.000000000',
             '2000-01-01T13:40:00.000000000', '2000-01-01T14:00:00.000000000',
             '2000-01-01T14:20:00.000000000', '2000-01-01T14:40:00.000000000',
             '2000-01-01T15:00:00.000000000', '2000-01-01T15:20:00.000000000',
             '2000-01-01T15:40:00.000000000', '2000-01-01T16:00:00.000000000',
             '2000-01-01T16:20:00.000000000', '2000-01-01T16:40:00.000000000',
             '2000-01-01T17:00:00.000000000', '2000-01-01T17:20:00.000000000',
             '2000-01-01T17:40:00.000000000', '2000-01-01T18:00:00.000000000',
             '2000-01-01T18:20:00.000000000', '2000-01-01T18:40:00.000000000',
             '2000-01-01T19:00:00.000000000', '2000-01-01T19:20:00.000000000',
             '2000-01-01T19:40:00.000000000', '2000-01-01T20:00:00.000000000',
             '2000-01-01T20:20:00.000000000', '2000-01-01T20:40:00.000000000',
             '2000-01-01T21:00:00.000000000', '2000-01-01T21:20:00.000000000',
             '2000-01-01T21:40:00.000000000', '2000-01-01T22:00:00.000000000',
             '2000-01-01T22:20:00.000000000', '2000-01-01T22:40:00.000000000',
             '2000-01-01T23:00:00.000000000', '2000-01-01T23:20:00.000000000',
             '2000-01-01T23:40:00.000000000', '2000-01-02T00:00:00.000000000',
             '2000-01-02T00:20:00.000000000', '2000-01-02T00:40:00.000000000',
             '2000-01-02T01:00:00.000000000', '2000-01-02T01:20:00.000000000',
             '2000-01-02T01:40:00.000000000', '2000-01-02T02:00:00.000000000',
             '2000-01-02T02:20:00.000000000', '2000-01-02T02:40:00.000000000',
             '2000-01-02T03:00:00.000000000', '2000-01-02T03:20:00.000000000',
             '2000-01-02T03:40:00.000000000', '2000-01-02T04:00:00.000000000',
             '2000-01-02T04:20:00.000000000', '2000-01-02T04:40:00.000000000',
             '2000-01-02T05:00:00.000000000', '2000-01-02T05:20:00.000000000',
             '2000-01-02T05:40:00.000000000', '2000-01-02T06:00:00.000000000',
             '2000-01-02T06:20:00.000000000', '2000-01-02T06:40:00.000000000',
             '2000-01-02T07:00:00.000000000', '2000-01-02T07:20:00.000000000',
             '2000-01-02T07:40:00.000000000', '2000-01-02T08:00:00.000000000',
             '2000-01-02T08:20:00.000000000', '2000-01-02T08:40:00.000000000',
             '2000-01-02T09:00:00.000000000', '2000-01-02T09:20:00.000000000',
             '2000-01-02T09:40:00.000000000', '2000-01-02T10:00:00.000000000',
             '2000-01-02T10:20:00.000000000', '2000-01-02T10:40:00.000000000',
             '2000-01-02T11:00:00.000000000', '2000-01-02T11:20:00.000000000',
             '2000-01-02T11:40:00.000000000', '2000-01-02T12:00:00.000000000',
             '2000-01-02T12:20:00.000000000', '2000-01-02T12:40:00.000000000',
             '2000-01-02T13:00:00.000000000', '2000-01-02T13:20:00.000000000',
             '2000-01-02T13:40:00.000000000', '2000-01-02T14:00:00.000000000',
             '2000-01-02T14:20:00.000000000', '2000-01-02T14:40:00.000000000',
             '2000-01-02T15:00:00.000000000', '2000-01-02T15:20:00.000000000',
             '2000-01-02T15:40:00.000000000', '2000-01-02T16:00:00.000000000'],
            dtype='datetime64[ns]')
    • salinity
      (time, nSCHISM_hgrid_node, nSCHISM_vgrid_layers)
      float32
      dask.array<chunksize=(72, 2639, 11), meta=np.ndarray>
      i23d :
      2
      coordinates :
      SCHISM_hgrid_node_x SCHISM_hgrid_node_y
      location :
      node
      grid_mapping :
      crs
      mesh :
      SCHISM_hgrid
      Array Chunk
      Bytes 15.95 MiB 7.97 MiB
      Shape (144, 2639, 11) (72, 2639, 11)
      Dask graph 2 chunks in 5 graph layers
      Data type float32 numpy.ndarray
      11 2639 144
    • time
      PandasIndex
      PandasIndex(DatetimeIndex(['1999-12-31 16:20:00', '1999-12-31 16:40:00',
                     '1999-12-31 17:00:00', '1999-12-31 17:20:00',
                     '1999-12-31 17:40:00', '1999-12-31 18:00:00',
                     '1999-12-31 18:20:00', '1999-12-31 18:40:00',
                     '1999-12-31 19:00:00', '1999-12-31 19:20:00',
                     ...
                     '2000-01-02 13:00:00', '2000-01-02 13:20:00',
                     '2000-01-02 13:40:00', '2000-01-02 14:00:00',
                     '2000-01-02 14:20:00', '2000-01-02 14:40:00',
                     '2000-01-02 15:00:00', '2000-01-02 15:20:00',
                     '2000-01-02 15:40:00', '2000-01-02 16:00:00'],
                    dtype='datetime64[ns]', name='time', length=144, freq=None))
In [5]:
#!conda install -y -c cadwr-dms schimpy
In [6]:
import schimpy

from schimpy import schism_mesh
In [7]:
import holoviews as hv
from holoviews import opts, dim
In [8]:
import holoviews.operation.datashader as hd
from holoviews.operation.datashader import datashade, rasterize
import warnings
warnings.filterwarnings('ignore')
In [9]:
smesh = schism_mesh.read_mesh('../tests/data/m1_hello_schism/hgrid.gr3')
In [10]:
smesh.n_nodes(), smesh.n_edges(), smesh.n_elems()
Out[10]:
(2639, 7274, 4636)
In [11]:
varname
Out[11]:
'salinity'
In [12]:
nodes = pd.DataFrame(smesh.nodes,columns=['x','y','z'])
nodes
nodes[varname] = ds[varname].values[0,:,0]
In [13]:
trimesh = hv.TriMesh((smesh.elems, hv.Points(nodes, vdims=varname)))
In [14]:
trimesh = trimesh.opts(
    opts.TriMesh(cmap='rainbow4', cnorm='eq_hist', colorbar=True, node_alpha=0, edge_alpha=0, edge_color=varname, filled=True, height=400, 
                 inspection_policy='edges', tools=['hover'], width=400))
In [15]:
trimesh.nodes.data
Out[15]:
x y index z salinity
0 56000.00 -10400.00 0 0.500000 3.364312e-02
1 55831.58 -10400.00 1 0.500000 1.095413e-02
2 56000.00 -10350.00 2 0.944444 2.154961e-02
3 55663.16 -10400.00 3 0.500000 2.462938e-08
4 55831.58 -10348.07 4 1.023290 5.119039e-03
... ... ... ... ... ...
2634 55831.58 10348.07 2634 1.023290 5.119046e-03
2635 55663.16 10400.00 2635 0.500000 2.464225e-08
2636 56000.00 10350.00 2636 0.944444 2.154963e-02
2637 55831.58 10400.00 2637 0.500000 1.095410e-02
2638 56000.00 10400.00 2638 0.500000 3.364312e-02

2639 rows × 5 columns

In [16]:
import datashader
In [17]:
def update(time=0, depth=0):
    trimesh.nodes.data[varname] = ds[varname].values[time, :, depth]
    return rasterize(trimesh, precompute=False, dynamic=True, 
                     aggregator=datashader.mean(varname)).opts(
        colorbar=True, cmap='rainbow4', # clim=(0,10), 
        width=600, tools=['hover'])
In [18]:
import panel as pn
pn.extension()
In [19]:
time_slider= pn.widgets.DiscreteSlider(name='Time',
                                       options=dict(zip(ds.time.astype('str').values,
                                                        range(len(ds.time)))))
depth_slider= pn.widgets.Select(name='nSCHISM_vgrid_layers',
                                options=dict(zip(ds.nSCHISM_vgrid_layers.astype('str').values, 
                                                 range(len(ds.nSCHISM_vgrid_layers)))))
In [20]:
slider_row = pn.Row(time_slider, depth_slider)
In [21]:
map_row = pn.Row(pn.bind(update, time=time_slider, depth=depth_slider))
In [22]:
dash = pn.Column(slider_row, map_row)
In [23]:
dash.servable(f'HelloSCHISM: {varname}')
Out[23]:
In [ ]: